<?php
/**
 * @copyright        2016 opencart.cn - All Rights Reserved
 * @link             http://www.guangdawangluo.com
 * @author           TL <mengwb@opencart.cn>
 * @created          2016-12-13 17:04:00
 * @modified         2016-12-13 17:04:00
 */

class ControllerWeixinloginIndex extends Controller
{
    public function index()
    {
        $route = '';
        if (!$this->config->get('weipay_status')) {
            return;
        }

        if (isset($this->request->get['route'])) {
            $part = explode('/', $this->request->get['route']);

            if (isset($part[0])) {
                $route .= $part[0];
            }
        }
        if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false && $route != 'weixinlogin' && (!isset($this->request->get['route']) || ($this->request->get['route'] != 'account/logout'))) {
            if (isset($this->session->data['userinfo']) && !$this->customer->isLogged()) {
                $this->load->model('account/weixin_login');
                //执行到此已经可以保证有$this->session->data['weixin_openid']的值了
                if($this->model_account_weixin_login->isOpenidExist($this->session->data['userinfo']['openid']) || $this->model_account_weixin_login->isUnionidExist($this->session->data['userinfo']['unionid'])) { //当前openid已经有账号
                    if (isset($this->session->data['userinfo']['unionid']) && !$this->model_account_weixin_login->isUnionidExist($this->session->data['userinfo']['unionid'])) {
                        $this->model_account_weixin_login->updateCustomerUnionid($this->session->data['userinfo']['openid'], $this->session->data['userinfo']['unionid']);
                    } else if ($this->model_account_weixin_login->isUnionidExist($this->session->data['userinfo']['unionid'])) { //此时可能绑定的是微信扫码支付的账号,openid是开放平台的openid，将其更新成公众平台的openid
                        $this->model_account_weixin_login->updateCustomerOpenid($this->session->data['userinfo']['unionid'], $this->session->data['userinfo']['openid']);
                    }
                    $this->model_account_weixin_login->updateCustomerHeadimgurl($this->session->data['userinfo']['openid'], $this->session->data['userinfo']['headimgurl']);
                    $this->weixinlogin();
                    $url = $this->session->data['wxredirect'];
                    unset($this->session->data['wxredirect']);
                    header('Location: ' . $url);
                } else {
                    //弹出微信登陆选择页面，让用户选择“PC端已注册账号”还是“首次访问本站”。
                    $this->response->redirect($this->url->link('weixinlogin/sel', '', 'SSL'));
                }
            }
        }
    }

    /*
    * 如果openid用户存在则登陆，否则不登陆
    * 需在customer表中增加字段openid
    */
    public function weixinlogin()
    {
        //如果openid用户存在则登陆，否则不登陆
        $openid = $this->session->data['userinfo']['openid'];
        $this->load->model('account/weixin_login');
        if($openid){
            $this->customer->logout();
            $this->cart->clear();

            unset($this->session->data['wishlist']);
            unset($this->session->data['payment_address']);
            unset($this->session->data['payment_method']);
            unset($this->session->data['payment_methods']);
            unset($this->session->data['shipping_address']);
            unset($this->session->data['shipping_method']);
            unset($this->session->data['shipping_methods']);
            unset($this->session->data['comment']);
            unset($this->session->data['order_id']);
            unset($this->session->data['coupon']);
            unset($this->session->data['reward']);
            unset($this->session->data['voucher']);
            unset($this->session->data['vouchers']);
            unset($this->session->data['guest']);
            if (isset($this->session->data['userinfo']['unionid']) && $this->session->data['userinfo']['unionid']) {
                $customer_info = $this->model_account_weixin_login->getCustomerInfoByUid($this->session->data['userinfo']['unionid']);
            } else {
                $customer_info = $this->model_account_weixin_login->getCustomerInfoByOpenid($openid);
            }
            $this->customer->login($customer_info['customer_id'], '', true);
      }
  }
}
